﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HtmlReader;

namespace LastFmReader
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public DataTable TracksTable { get; private set; }

        private void LastFmBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            HtmlDocument document = new HtmlDocument(LastFmBrowser.Document);
            HtmlElementCollection tables = document.All.GetElementsByTagName("TABLE");
            HtmlElement table = tables.GetElementById("recentTracks");
            HtmlElement tbody = table.FirstChild;
            foreach (HtmlElement tr in tbody.Children)
            {
                HtmlElement td = tr.Children[2];
                string artist = td.Children[0].InnerText;
                string Title = td.Children[1].InnerText;
                DataRow row = TracksTable.NewRow();
                row["Artist"] = artist;
                row["Title"] = Title;
                TracksTable.Rows.Add(row);
            }
            TracksListBox.ItemsSource = TracksTable.DefaultView;
            this.Cursor = Cursors.Arrow;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TracksTable = new DataTable();
            TracksTable.Columns.Add("Artist");
            TracksTable.Columns.Add("Title");
        }
    }
}
